from sqlalchemy.orm import aliased

from apps.settingApi.api.role.model import roleModel, rolePermissionModel,roleUserModel
from apps.settingApi.api.funs.model import funsModel
from apps.settingApi.api.users.model import permissionModel
from apps.settingApi.api.lessees.model import lesseesFunsModel
from apps.settingApi.permission import permissionDict1, barList
from comm.sqlalchemyConfig import db

def userPermission(userId,lesseesId,userType,lesseesType):
    permissionDictData = permissionDict1()
    permission = {}
    for itm1 in permissionDictData.keys():

        for itm2 in permissionDictData[itm1]['list']:
            permission[itm1+'-'+itm2['method']] = False
    # 配置菜单模块
    barListData = barList()
    # 配置模块菜单
    for itm in barListData:
        permission[itm['code']] = False

    # 配置管理员权限
    if userType==1:
        for itm in permission.keys():
            permission[itm] = True
        # 配置模块菜单
        # 经销商开放全部菜单
        if lesseesType==1:
            for itm in barListData:
                permission[itm['code']] = True
        else:
            # 非经销商开放部分菜单
            for itm in barListData:
                permission[itm['code']] = False
            # 查询开放的模块
            z_funs = db.session.query(lesseesFunsModel).filter(lesseesFunsModel.lessees_id == lesseesId).all()
            for itm in z_funs:
                permission[itm.funs_code]=True

    # 配置按钮权限
    dataObj = db.session.query(rolePermissionModel.permission_code, permissionModel.funs_code, funsModel.code).filter(
        funsModel.status == 1,
        lesseesFunsModel.funs_code == funsModel.code,
        permissionModel.status == 1,
        rolePermissionModel.permission_code == permissionModel.code,
        roleModel.id == rolePermissionModel.role_id,
        roleModel.id == roleUserModel.role_id,
        roleUserModel.user_id == userId,
        roleUserModel.lessees_id == lesseesId,
        lesseesFunsModel.lessees_id == lesseesId,
        roleModel.lessees_id == lesseesId,
        rolePermissionModel.lessees_id == lesseesId).all()

    # 配置按钮权限
    # 将模块表增加一个别名
    # rolePermissionModel.lessees_id==lesseesId).all()
    # aliasedFunsModel = aliased(funsModel)
    # dataObj = db.session.query(rolePermissionModel.permission_code, permissionModel.funs_code, funsModel.code).filter(
    #     funsModel.status == 1,
    #     aliasedFunsModel.parent_code == funsModel.code,
    #     lesseesFunsModel.funs_code == funsModel.code,
    #     permissionModel.status == 1,
    #     aliasedFunsModel.code == permissionModel.funs_code,
    #     rolePermissionModel.permission_code == permissionModel.code,
    #     roleModel.id == rolePermissionModel.role_id,
    #     roleModel.id == roleUserModel.role_id,
    #     roleUserModel.user_id == userId,
    #     roleUserModel.lessees_id == lesseesId,
    #     lesseesFunsModel.lessees_id == lesseesId,
    #     roleModel.lessees_id == lesseesId,
    #     rolePermissionModel.lessees_id == lesseesId).all()

    for itm1 in permission.keys():
        for itm2 in dataObj:
            if itm1==itm2.permission_code:
                permission[itm1]=True
                permission[itm2.code] = True
    return permission

def userUrlPermission(userId,lesseesId,code):
    # 先判断是否在权限表里
    # 在权限表在判断用户是否有权限
    # 不在权限表则直接返回True
    p = db.session.query(permissionModel).filter(permissionModel.code==code).first()
    if p is None:
        return True
    dataObj = db.session.query(rolePermissionModel).filter(funsModel.status == 1, permissionModel.status == 1,
                                                           funsModel.code == permissionModel.funs_code,
                                                           rolePermissionModel.permission_code == permissionModel.code,
                                                           roleModel.id == rolePermissionModel.role_id,
                                                           roleModel.id == roleUserModel.role_id,
                                                           roleUserModel.user_id == userId,
                                                           roleUserModel.lessees_id == lesseesId,rolePermissionModel.permission_code==code).first()
    if dataObj is None:
        return False
    else:
        return True